package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.datatransferobject.UserAddParam;
import cn.tedu.eguser1.pojo.datatransferobject.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.valueobject.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.tedu.eguser1.base.response.JsonResult.ok;

@Tag(name = "01用户资讯模块")
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "用户密码"),
            @Parameter(name = "nickname", description = "用户昵称"),
            @Parameter(name = "userAddParam", hidden = true)})
    @Operation(summary = "添加用户资讯")
    @PostMapping("/add")
    public JsonResult userAdd(@Validated UserAddParam userAddParam) {
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new

                Date());
        user.setUpdateTime(new

                Date());
        int num = userMapper.addUser(user);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "用户资讯详情")
    @GetMapping("/list")
    public JsonResult selectUser() {
        List<UserListVO> listVOS = userMapper.listUser();
        return JsonResult.ok(listVOS);
    }

    @Parameters(value = {
            @Parameter(name = "id", description = "用户名id", required = true, example = "12"),
            @Parameter(name = "username", description = "用户名", required = true, example = "芭比")})
    @ApiOperationSupport(order = 30)
    @Operation(summary = "删除用户资讯")
    @PostMapping("/del")
    public JsonResult deleteUsers(Long id) {
        int num = userMapper.deleteUser(id);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @Parameter(name = "userUpdateParam", hidden = true)
    @ApiOperationSupport(order = 40)
    @Operation(summary = "修改用户资讯")
    @PostMapping("/update")
    public JsonResult updateUserMessage(UserUpdateParam userUpdateParam) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int num = userMapper.updateUser(user);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
